package com.bw.controller;

import cn.hutool.core.util.IdUtil;
import com.bw.domain.*;
import com.bw.service.*;
import com.bw.util.Base64Util;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("farmers")
public class FarmerController {
    @Autowired
    TraceTransportService traceTransportService;
    @Autowired
    TraceUserCropsService traceUserCropsService;
    @Autowired
    SysDictDataService sysDictDataService;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    SysDeptService sysDeptService;
    @Value("${fdfs.address}")
    private String address;

    @GetMapping("list")
    public Map<String, Object> list() {
        List<TraceUserCrops> traceTransport = traceUserCropsService.lists();
        Map<String, Object> map = new HashMap<>();
        map.put("list", traceTransport);
        return map;
    }

    @GetMapping("getTypes")
    public Map<String, Object> getTypes(String type) {
        Map<String, Object> map = new HashMap<>();
        List<SysDictData> list = sysDictDataService.getTypes(type);
        map.put("list", list);
        return map;
    }
    @PostMapping("saveCrops")
    public void saveCrops(@RequestBody TraceUserCrops traceUserCrops) {
//        traceUserCrops.setCropsId(IdUtil.getSnowflakeNextIdStr());
//        traceUserCrops.setStatus(0);
        int i = traceUserCropsService.saveCrops(traceUserCrops);
    }
    @Autowired
    SysUserService sysUserService;
    @GetMapping("sj")
    public List<SysUser> sj(String id) {
        List<SysUser> list  = sysUserService.getsj(id);
        return list;
    }

    @GetMapping("cs")
    public List<SysDept> cs(Long cs) {
        List<SysDept> list  = sysDeptService.getcs(cs);
        System.out.println(list);
        return list;
    }
    @GetMapping("getzwxq")
    public TraceUserCrops getzwxq(Long id) {
       TraceUserCrops traceUserCrops = traceUserCropsService.getxq(id);
        return traceUserCrops;
    }
    @PostMapping("addTrasport")
    public Map<String, Object> addTrasport(@RequestBody TraceTransport traceTransport) {
        Map<String, Object> map = new HashMap<>();
//        SysDept sysDept = sysDeptService.getFarmerDept(traceTransport.getFactoryId());
        traceTransport.setFarmerDept("织金小李竹荪种植基地");
//        System.out.println("ssssssss"+sysDept);
        SysUser sysUser = sysUserService.getuxser(traceTransport.getFarmerUserName());
        traceTransport.setFarmerNickName(sysUser.getNickName());
        traceTransport.setFarmerTel(sysUser.getPhonenumber());

        int i = traceTransportService.addTrasport(traceTransport);
        map.put("msg","成功");
        return map;
    }

    @PostMapping("imageUpload")
    public Map<String ,Object> imageUpload(@RequestBody CropsImageVo cropsImageVo) throws FileNotFoundException {
        //替换掉部分图片字符串信息
        String imageBase64 = cropsImageVo.getImageBase64().replace("data:image/jpeg;base64,", "");
        //保存图片并返回图片文件对象
        File file = Base64Util.base64ConvertFile(imageBase64);
        StorePath storePath = this.fastFileStorageClient.uploadImageAndCrtThumbImage(new FileInputStream(file),file.length(),"jpg",null);
        Map<String,Object> map = new HashMap<>();
        map.put("msg",address+storePath.getFullPath());
        return map;
    }
    @GetMapping("del")
    public Map<String ,Object> del(String id) throws FileNotFoundException {
        Map<String, Object> map = new HashMap<>();
        String[] split = id.split(",");
        List<String> list = Arrays.asList(split);
        traceUserCropsService.removeBatchByIds(list);
        map.put("msg", "删除成功");
        map.put("code", 200);
        return map;
    }
}
